/* typehints:start */
import { GameRoot } from "../root";
/* typehints:end */

import { animRemoveElement } from "../../core/utils/utils";

export class DynamicDom{
  constructor(root, element){
    /** @type {GameRoot} */
    this.root = root;

    /** @type {HTMLElement} */
    this.element = element;

    this.parent = this.element.parentElement;

    this.attached = true;
    
    this.element.classList.remove('remove')
    this.internalDetach(false);
  }

  internalDetach(isAnimHide){
    if(this.attached){
      this.attached = false;

      if(isAnimHide){
        animRemoveElement(this.element, () => {
          this.element.parentElement.removeChild(this.element);
        })
      } else {
        this.element.parentElement.removeChild(this.element);
      }
    }
  }

  internalAttach(){
    if(!this.attached){
      this.parent.appendChild(this.element);
      this.attached = true;
    }
  }
}